package com.hadoop.project.mvc.controller;

import com.hadoop.project.entity.constant.Constant;
import com.hadoop.project.entity.dto.UserEditDTO;
import com.hadoop.project.entity.dto.UserLoginDTO;
import com.hadoop.project.entity.vo.ResponseVO;
import com.hadoop.project.entity.vo.UserLoginVO;
import com.hadoop.project.mvc.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Api(tags = "用户相关接口")
@RequiredArgsConstructor
public class UserController extends ABaseController{

    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    private final UserService userService;


    /**
     * 用户登录
     * @param userLoginDto
     * @return
     */
    @ApiOperation(value = "用户登录接口")
    @PostMapping("/login")
    public ResponseVO<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDto){
        log.info("用户提交的信息为：{}",userLoginDto);
        UserLoginVO UserLoginVO = userService.login(userLoginDto);
        return getSuccessResponseVO(UserLoginVO);
    }

    /**
     * 发送验证码
     * @param email 邮箱
     * @return
     */
    @ApiOperation(value = "发送验证码接口")
    @PostMapping("/sendEmail")
    public ResponseVO sendEmail(@RequestParam("email") String email){
        userService.getVerificatioCode(email, Constant.DEFAULT_CODE_LENGTH);
        return getSuccessResponseVO(Constant.OPERATION_SUCCESS);
    }

    /**
     * 用户注册
     * @param userLoginDto
     * @return
     */
    @ApiOperation(value = "用户注册接口")
    @PostMapping("/register")
    public ResponseVO addUser(@RequestBody UserLoginDTO userLoginDto){
        userService.register(userLoginDto);
        return getSuccessResponseVO(Constant.OPERATION_SUCCESS);
    }

    /**
     * 忘记密码
     * @return
     */
    @ApiOperation(value = "忘记密码接口")
    @PutMapping("/changePassword")
    public ResponseVO changePassword(@RequestBody UserLoginDTO userLoginDto){
        log.info("用户提交的信息为：{}",userLoginDto);
        userService.modifyPassword(userLoginDto);
        return getSuccessResponseVO(Constant.OPERATION_SUCCESS);
    }

    /**
     * 添加密码或修改用户名
     * @param userEditDto
     */
    @ApiOperation(value = "添加密码或修改用户名接口")
    @PutMapping("/editInfo")
    public ResponseVO insertPassword(@RequestBody UserEditDTO userEditDto){
        userService.editInfo(userEditDto);
        return getSuccessResponseVO(Constant.OPERATION_SUCCESS);
    }
}
